<script setup>
import { onMounted, ref } from 'vue'
import dial from './dial.vue'
import { getAllApi } from '@/api/stocktaking/task'
// import { getAllApi, deleteApi, toggleEnableApi } from '@/api/stocktaking/task'

const formData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const size = ref('default')
const background = ref(true)
const disabled = ref(false)

// 搜索表单
const form = ref({
  taskName: '',
  category: '',
  isEnable: ''
})

// 盘点类型选项
const categoryOptions = [
  { value: 1, label: '仓库盘点' },
  { value: 2, label: '库区盘点' },
  { value: 3, label: '库位盘点' }
]

// 启用状态选项
const enableOptions = [
  { value: 1, label: '启用' },
  { value: 2, label: '停用' }
]

// 弹窗引用
const diaRef = ref(null)

// 分页相关方法
const handleSizeChange = (val) => {
  pageSize.value = val
  getAll()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getAll()
}

// 获取数据
const getAll = async () => {
  // 模拟数据获取
  let res = await getAllApi(
    form.value.taskName,
    currentPage.value,
    pageSize.value
  )
  total.value = res.total
  formData.value = res.rows
}

// 搜索
const search = () => {
  currentPage.value = 1
  getAll()
}

// 重置搜索
const resetSearch = () => {
  form.value = {
    taskName: '',
    category: '',
    isEnable: ''
  }
  search()
}

// 添加
const handleAdd = () => {
  console.log('111')
  diaRef.value.resetForm()
  diaRef.value.dialogVisible = true
}

// 编辑
const handleEdit = (row) => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    // 模拟数据回显
    diaRef.value.dialogFormData = { ...row }
  }
}

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除这条任务吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // await deleteApi(id)
    ElMessage.success('删除成功')
    getAll()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 查看详情
const handleViewDetails = (row) => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    diaRef.value.dialogFormData = { ...row }
    diaRef.value.isViewMode = true
  }
}

// 启用/停用任务
const toggleEnable = async (row) => {
  try {
    // await toggleEnableApi(row.id, row.isEnable === 1 ? 2 : 1)
    row.isEnable = row.isEnable === 1 ? 2 : 1
    ElMessage.success('操作成功')
    getAll()
  } catch (error) {
    ElMessage.error('操作失败: ' + error.message)
  }
}

// 查看任务详情
const viewTaskDetails = (row) => {
  // 跳转到任务详情页面
  // router.push(`/stocktaking/taskdetails?taskId=${row.id}`)
  ElMessage.info('查看任务详情功能待实现')
}

onMounted(() => {
  getAll()
})
</script>

<template>
  <dial ref="diaRef" @refresh="getAll" />

  <!-- 搜索表单 -->
  <el-form :model="form" :inline="true" class="search-form">
    <el-form-item label="任务名称">
      <el-input v-model="form.taskName" placeholder="请输入任务名称" />
    </el-form-item>
    <el-form-item label="盘点类型">
      <el-select v-model="form.category" placeholder="请选择盘点类型" clearable>
        <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="状态">
      <el-select v-model="form.isEnable" placeholder="请选择状态" clearable>
        <el-option v-for="item in enableOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </el-form-item>
  </el-form>

  <!-- 操作按钮 -->
  <div class="toolbar">
    <el-button type="primary" @click="handleAdd">新增任务</el-button>
  </div>

  <!-- 数据表格 -->
  <el-table :data="formData" style="width: 100%" border>
    <el-table-column prop="id" label="主键ID" width="100" />
    <el-table-column prop="taskName" label="任务名称" width="150" />
    <el-table-column prop="cronExpression" label="执行时间表达式" width="150" />
    <el-table-column prop="startTime" label="任务执行时间" width="180" />
    <el-table-column prop="category" label="盘点类型" width="120">
      <template #default="{ row }">
        <el-tag :type="row.category === 1 ? 'primary' : row.category === 2 ? 'success' : 'warning'">
          {{ row.category === 1 ? '仓库盘点' : row.category === 2 ? '库区盘点' : '库位盘点' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="empId" label="员工ID" width="100" />
    <el-table-column prop="isEnable" label="状态" width="100">
      <template #default="{ row }">
        <el-tag :type="row.isEnable === 1 ? 'success' : 'info'">
          {{ row.isEnable === 1 ? '启用' : '停用' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" width="180" />
    <el-table-column prop="createBy" label="创建人" width="120" />
    <el-table-column fixed="right" label="操作" width="280">
      <template #default="{ row }">
        <el-button type="primary" size="small" @click="handleViewDetails(row)">查看</el-button>
        <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
        <el-button :type="row.isEnable === 1 ? 'warning' : 'success'" size="small" @click="toggleEnable(row)">
          {{ row.isEnable === 1 ? '停用' : '启用' }}
        </el-button>
        <!-- <el-button type="info" size="small" @click="viewTaskDetails(row)">详情</el-button> -->
        <el-button type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <div class="pagination-container">
    <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 50]"
      :size="size" :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper"
      :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<style scoped>
.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.toolbar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>